package gui_layer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;

import model_layer.Sale;
import model_layer.StaffMember;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -------------------
 * LineChartDemo1.java
 * -------------------
 * (C) Copyright 2002-2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: LineChartDemo1.java,v 1.27 2004/05/27 09:10:42 mungady Exp $
 *
 * Changes
 * -------
 * 08-Apr-2002 : Version 1 (DG);
 * 30-May-2002 : Modified to display values on the chart (DG);
 * 25-Jun-2002 : Removed redundant import (DG);
 * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
 *
 */
@SuppressWarnings("serial")
public class ChartCreator extends JInternalFrame
{
	private StaffMember sm;

	/**
	 * Create the frame.
	 */
	public ChartCreator(StaffMember staff_member)
	{
		super("Sales of " + staff_member.getFname() + " " + staff_member.getLname());
		try
		{
			setFrameIcon(new ImageIcon(ChartCreator.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{	
		}
		setResizable(true);
		setMaximizable(true);
		setIconifiable(true);
		setClosable(true);
		sm = staff_member;
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		final JFreeChart chart = createChart(create_dataset());
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
	}
	
	
	private CategoryDataset create_dataset()
	{
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		final String row_key = sm.getFname() + " " + sm.getLname();
		Map<Integer, Float> data = new TreeMap<Integer, Float>();
		Calendar cal = new GregorianCalendar();
		Calendar today = new  GregorianCalendar();
		for (Sale sale : sm.getMade_sales())
		{
			if((today.get(Calendar.YEAR) - cal.get(Calendar.YEAR)) <= 1)
			{
				
				cal.setTime(sale.getSale_date());
				System.out.println(sale.getSale_date().getTime());
				int month = cal.get(Calendar.MONTH) + 1;
				System.out.println(month);
				float total_monthly_sales= sale.getTotal_price();
				if(data.containsKey(month))
				{
					System.out.println("Contains " + month);
					total_monthly_sales += data.get(month);
					System.out.println(total_monthly_sales);
				}
				data.put(month, total_monthly_sales);
			}
		}
		//cal.setTime(today);
		int j =  today.get(Calendar.MONTH) + 2;
		for(int i = 0; i <= 12; i++)
		{
			if(data.containsKey(j))
			{
				dataset.addValue(data.get(j), row_key, String.valueOf(j));
			}
			else
			{
				dataset.addValue(0, row_key, String.valueOf(j));
			}
			j %= 12;
			j++;
			
		}
		return dataset;
	}
	
	 private JFreeChart createChart(final CategoryDataset dataset)
	 {
		 final JFreeChart chart = ChartFactory.createLineChart(
		            "Sales",       // chart title
		            "Month",                    // domain axis label
		            "Total Sales",                   // range axis label
		            dataset,                   // data
		            PlotOrientation.VERTICAL,  // orientation
		            false,                      // include legend
		            true,                      // tooltips
		            false                      // urls
		        );
		 float[] values = Color.RGBtoHSB(255, 104, 0, null);
		 chart.setBackgroundPaint(Color.getHSBColor(312, 0.11f, 0.17f));
		 chart.getTitle().setFont(new Font("Helvetica Neue", Font.BOLD, 20));
		 chart.getTitle().setPaint(Color.WHITE);

        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.getHSBColor(312, 0.11f, 0.17f));
        CategoryItemRenderer rend = (CategoryItemRenderer) plot.getRenderer();
        rend.setSeriesPaint(0, Color.getHSBColor(values[0], values[1], values[2]));
        
        final CategoryAxis category_axis = (CategoryAxis) plot.getDomainAxis();
        category_axis.setTickLabelFont(new Font("Helvetica Neue", Font.BOLD, 11));
        category_axis.setTickLabelPaint(Color.WHITE);
        category_axis.setLabelFont(new Font("Helvetica Neue", Font.BOLD, 15));
        category_axis.setLabelPaint(Color.WHITE);

        // customise the range axis...
        final NumberAxis range_axis = (NumberAxis) plot.getRangeAxis();
        range_axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        range_axis.setAutoRangeIncludesZero(true);
        range_axis.setTickLabelFont(new Font("Helvetica Neue", Font.BOLD, 11));
        range_axis.setTickLabelPaint(Color.WHITE);
        range_axis.setLabelFont(new Font("Helvetica Neue", Font.BOLD, 15));
        range_axis.setLabelPaint(Color.WHITE);
        
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setSeriesFillPaint(0, Color.getHSBColor(values[0], values[1], values[2]));
        renderer.setSeriesStroke(0, new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0f));
      
      return chart;
	 }
	

}
